// 登陆注册状态管理

import { requestRegister, requestLogin } from '@/api/api'

export default {
    namespaced: true,
    actions: {
        // 注册
        async requestRegister(content, user) {
            try {
                if (user.username === '' || user.password === '' || user.phone === '') {
                    content.commit('REGISTERTIPS', '任何一项不可输入为空！')
                } else {
                    const res = await requestRegister(user.username, user.phone, user.password)
                    content.commit('REGISTERTIPS', res.msg)
                }
            } catch (e) {
                console.log(e)
            }
        },

        // 登陆
        async requestLogin(content, user) {
            try {
                if (user.username === '' || user.phone === '') {
                    content.commit('REGISTERTIPS', '任何一项不可输入为空！')
                } else {
                    const res = await requestLogin(user.phone, user.password)
                    console.log(res)

                    sessionStorage.setItem('user',JSON.stringify(res.data))
                    
                    content.commit('REGISTERTIPS',res.msg)
                }
            } catch (e) {
                console.log(e)
            }
        }

    },
    mutations: {
        REGISTERTIPS(state, val) {
            state.tips.text = val
            state.tips.isTips = true
            setTimeout(() => {
                state.tips.text = ''
                state.tips.isTips = false
            }, 1000)
        }
    },
    state: {
        tips: {
            text: "",
            isTips: false
        },

    }
}